//console.log('This is a popup!');
// popup.js
// 当弹出页面的DOM加载完成时执行
document.addEventListener('DOMContentLoaded', function() {
    const createButton = document.getElementById('create');
    const contentInput = document.getElementById('content');
    const cardsContainer = document.getElementById('cards-container');

    // 从存储中加载已有卡片
    chrome.storage.local.get(['cards'], function(result) {
        const cards = result.cards || [];
        cards.forEach(cardContent => {
            createCard(cardContent);
        });
    });

    createButton.addEventListener('click', function() {
        const content = contentInput.value.trim();
        if (content) {
            // 保存到存储
            chrome.storage.local.get(['cards'], function(result) {
                const cards = result.cards || [];
                cards.push(content);
                chrome.storage.local.set({ cards: cards });
                
                // 创建新卡片
                createCard(content);
            });
            
            // 清空输入
            contentInput.value = '';
        }
    });

    function createCard(content) {
        const card = document.createElement('div');
        card.className = 'card';
        
        const text = document.createElement('p');
        text.textContent = content;
        
        const deleteBtn = document.createElement('button');
        deleteBtn.textContent = '删除';
        deleteBtn.style.backgroundColor = '#f44336';
        deleteBtn.style.marginTop = '10px';
        
        deleteBtn.addEventListener('click', function() {
            // 从存储中删除
            chrome.storage.local.get(['cards'], function(result) {
                const cards = result.cards || [];
                const index = cards.indexOf(content);
                if (index > -1) {
                    cards.splice(index, 1);
                    chrome.storage.local.set({ cards: cards });
                }
            });
            
            // 从DOM中删除
            card.remove();
        });
        
        card.appendChild(text);
        card.appendChild(deleteBtn);
        cardsContainer.insertBefore(card, cardsContainer.firstChild);
    }
});

// 显示当前的时间
function displayTime() {
    var now = new Date();
    var timeString = now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds();
    document.getElementById('time').textContent = timeString;
}

